package model

import "time"

type JingdongOrder struct {
	Id int64 `xorm:"pk autoincr"`
	Commodity *Commodity
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Commodity struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	ColourList []*Colour `xorm:"-"`
	EditionList []*Edition `xorm:"-"`
	SelectCombination []*SelectCombination `xorm:"-"`
	PurchaseMethod []*PurchaseMethod `xorm:"-"`
	SecurityServiceList []*SecurityServices `xorm:"-"`
	JingdongServiceList []*JingdongService `xorm:"-"`
	ServiceList []interface{} `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Colour struct {
	Id int64 `xorm:"pk autoincr"`
	CommodityId int64
	Name string
	Choose bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Edition struct {
	Id int64 `xorm:"pk autoincr"`
	CommodityId int64
	Name string
	Choose bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SelectCombination struct {
	Id int64 `xorm:"pk autoincr"`
	CommodityId int64
	Name string
	Choose bool
	CreatedAt time.Time
	UpdatedAt time.Time
}
type PurchaseMethod struct {
	Id int64 `xorm:"pk autoincr"`
	CommodityId int64
	Name string
	Choose bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SecurityServices struct {
	Id int64 `xorm:"pk autoincr"`
	CommodityId int64
	Name string
	SecurityTypeList []*SecurityType
	CreatedAt time.Time
	UpdatedAt time.Time
}

type SecurityType struct {
	Id int64 `xorm:"pk autoincr"`
	SecurityServiceId int64
	Name string
	GuaranteeList []*Guarantee
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Guarantee struct {
	Id int64 `xorm:"pk autoincr"`
	SecurityTypeId int64
	Name string
	Price float64
	Choose bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

type JingdongType struct {
	Id int64 `xorm:"pk autoincr"`
	JingdongServiceId int64
	Name string
	ServiceList []*Service
	CreatedAt time.Time
	UpdatedAt time.Time
}

type JingdongService struct {
	Id int64 `xorm:"pk autoincr"`
	CommodityId int64
	Name string
	JingdongTypeList []*JingdongType
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Service struct {
	Id int64 `xorm:"pk autoincr"`
	JingdongTypeId int64
	Name string
	Price float64
	Choose bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

func PlaceOrder(cId int64)(){
	var c Commodity
	x.Id(cId).Get(&c)

	x.Where("commodity_id = ?", c.Id).Find(&c.ColourList)

	x.Where("commodity_id = ?", c.Id).Find(&c.EditionList)

	x.Where("commodity_id = ?", c.Id).Find(&c.SelectCombination)

	x.Where("commodity_id = ?", c.Id).Find(&c.PurchaseMethod)

	x.Where("commodity_id = ?", c.Id).Find(&c.SecurityServiceList)
	for _, ss := range c.SecurityServiceList{
		x.Find(&ss.SecurityTypeList)
		for _, st := range ss.SecurityTypeList{
			x.Find(st.GuaranteeList)
		}
	}

	x.Where("commodity_id = ?", c.Id).Find(&c.JingdongServiceList)
	for _, js := range c.JingdongServiceList{
		x.Find(&js.JingdongTypeList)
		for _, jt := range js.JingdongTypeList{
			x.Find(jt.ServiceList)
		}
	}
}

func QueryService(c *Commodity)(){
	for _, ss := range c.SecurityServiceList{
		for _, st := range ss.SecurityTypeList{
			for _, g := range st.GuaranteeList{
				if g.Choose{
					c.ServiceList = append(c.ServiceList, g)
				}
			}
		}
	}

	x.Where("commodity_id = ?", c.Id).Find(&c.JingdongServiceList)
	for _, js := range c.JingdongServiceList{
		for _, jt := range js.JingdongTypeList{
			for _, s := range jt.ServiceList{
				if s.Choose{
					c.ServiceList = append(c.ServiceList, s)
				}
			}
		}
	}
}
